/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.personal.controller;

import com.alibaba.fastjson.JSONArray;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.personal.domain.GroupMember;
import com.koron.personal.domain.CommonGroup;
import com.koron.personal.service.api.CommonGroupService;
import com.koron.system.utils.BusinessCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 */
@Api(tags = "commonGroups", description = "常用群组接口类")
@RestController
@RequestMapping("/commonGroups")
public class CommonGroupsController {

    @Autowired
    private CommonGroupService service;

    @ApiOperation("保存群组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "群组id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "群组名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "groupMembers", value = "群组成员json字符串", dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/save.htm", method = RequestMethod.POST)
    public MessageBean<Integer> save(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore CommonGroup group, @RequestParam String groupMembers) {
        MessageBean<Integer> msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NOT_LOGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return msg;
        }
        List<GroupMember> groupMemberList = new ArrayList<>();
        if (StringUtils.isNotEmpty(groupMembers)) {
            groupMemberList = JSONArray.parseArray(groupMembers, GroupMember.class);
        }
        group.setUpdateTime(new Date());
        group.setUpdateUser(user.getCode());
        group.setUpdateUserName(user.getName());
        msg = service.save(user, group, groupMemberList);
        return msg;
    }


    @ApiOperation("根据主键删除群组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "群组id:", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    public MessageBean<Integer> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            return MessageBean.create(BusinessCode.NOT_LOGIN.getCode(), BusinessCode.NOT_LOGIN.getDescription(), Integer.class);
        }
        MessageBean<Integer> msg = service.delete(id);
        return msg;
    }


    @ApiOperation("根据主键获取群组信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "群组id:", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/details.htm", method = RequestMethod.GET)
    public MessageBean<CommonGroup> details(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String id) {
        if (null == user) {
            return MessageBean.create(BusinessCode.NOT_LOGIN.getCode(), BusinessCode.NOT_LOGIN.getDescription(), CommonGroup.class);
        }
        MessageBean<CommonGroup> msg = service.queryOne(id);
        return msg;
    }


    @ApiOperation("列表查询用户的群组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "用户code:", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "群组名称:", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页:", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数:", dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    public MessageBean<DataBean<List<CommonGroup>>> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String userCode, @RequestParam(required = false) String name, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize) {
        if (null == user) {
            MessageBean<DataBean<List<CommonGroup>>> msg = new MessageBean();
            msg.setCode(BusinessCode.NOT_LOGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return msg;
        }
        MessageBean<DataBean<List<CommonGroup>>> msg = service.list(userCode, name, page, pageSize);
        return msg;
    }


    @ApiOperation("获取用户的所有群组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCode", value = "用户code:", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/getAllGroup.htm", method = RequestMethod.GET)
    public MessageBean<List> getAllGroup(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String userCode) {
        if (null == user) {
            return MessageBean.create(BusinessCode.NOT_LOGIN.getCode(), BusinessCode.NOT_LOGIN.getDescription(), List.class);
        }
        MessageBean<List> msg = service.queryAllGroup(userCode);
        return msg;
    }

}
